package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();

        PageHelper.startPage(currentPage, pageSize);

        Page<CheckGroup> checkGroups = checkGroupDao.findPage(queryString);


        return new PageResult(checkGroups.getTotal(), checkGroups.getResult());
    }

    @Override
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        checkGroupDao.add(checkGroup);
        System.out.println(checkGroup.getId());
        setT_checkgroup_checkitem(checkitemIds, checkGroup.getId());
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIds(Integer id) {
        return checkGroupDao.findCheckItemIds(id);
    }

    @Override
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        checkGroupDao.delCheckitemIdsById(checkGroup.getId());
        checkGroupDao.update(checkGroup);
        setT_checkgroup_checkitem(checkitemIds, checkGroup.getId());
    }

    @Override
    public Result del(Integer id) {
        Integer count = checkGroupDao.findCheckGroupIds(id);

        if (count != 0) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL_middle);
        } else {
            checkGroupDao.delCheckitemIdsById(id);
            checkGroupDao.del(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }
    }

    @Override
    public List<Setmeal> findAll() {
        return checkGroupDao.findAll();
    }

    public void setT_checkgroup_checkitem(Integer[] checkitemIds, Integer checkgroupId) {
        Map<String, Integer> map = new HashMap<>();
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                map.put("checkgroupId", checkgroupId);
                map.put("checkitemId", checkitemId);
                checkGroupDao.addMiddleTable(map);
            }
        }
    }
}
